<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2013
 * ---
 * Repraesentiert eine Kategorie
 *
 * @author Robin
 */
class Category {
    private $id = null;
    private $name = null;
    
    public function __construct($data = array()) {
        if (isset($data['category_id'])) $this->id = (int) $data['category_id'];
        if (isset($data['category_name'])) $this->name = $data['category_name'];
    }
    
    /**
     * 
     * @param type $id
     * @return \Category
     */
    public static function getCategoryById($id) {
        $db = new Database();

        $sql = "SELECT * FROM ff_categories WHERE category_id = :id";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $id, PDO::PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch();
        
        $db = null;

        if ($row) return new Category($row);
    }
    
    /**
     * 
     * @param type $numRows
     * @return type
     */
    public static function getCategoryList($numRows=1000000) {
        $db = new Database();

        $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM ff_categories LIMIT :numRows";

        $stmt = $db->prepare( $sql );
        $stmt->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
        $stmt->execute();
        
        $list = array();
        while ($row = $stmt->fetch()) {
            $category = new Category($row);
            $list[] = $category;
        }
        
        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;
        
        return (array("categoryList" => $list, "totalRows" => $totalRows[0]));
    }
    
    /**
     * 
     */
    public function insert() {
        if (!is_null($this->id)) {
            trigger_error("Category::insert(): Es wird versucht eine Kategorie zu speichern, die eine ID hat, die bereits existiert (ID: $this->id).", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "INSERT INTO ff_categories (category_name) VALUES (:name)";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":name", $this->name, PDO::PARAM_STR);
        $stmt->execute();
        
        $this->id = $db->lastInsertId();
        
        $db = null;
    }
    
    /**
     * 
     */
    public function update() {
        if (is_null($this->id)) {
            trigger_error("Category::update(): Es wird versucht eine Kategorie zu speichern, die keine ID hat - was nicht geht beim Update.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "UPDATE ff_categories SET category_name=:name WHERE category_id=:id";
        
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":name", $this->name, PDO::PARAM_STR);
        $stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
    
    /**
     * 
     */
    public function delete() {
        if (is_null($this->id)) {
            trigger_error("Category::delete(): Es wird versucht eine Kategorie zu loeschen, die keine ID hat - was nicht geht beim Loeschen.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "DELETE FROM ff_categories WHERE category_id = :id LIMIT 1";
        
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
    
    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }
}

?>
